home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / STDLIB.PAK / MERGE.CPP < prev    next >
Text File  |  1997-05-06  |  2KB  |  73 lines

  1.  #include <algorithm>
  2.  #include <vector>    
  3.  
  4.  using namespace std;
  5.  
  6.  int main ()
  7.  {
  8.    int d1[4] = {1,2,3,4};
  9.    int d2[8] = {11,13,15,17,12,14,16,18};
  10.    //
  11.    // Set up two vectors.
  12.    //
  13.    vector<int> v1(d1+0, d1+4), v2(d1+0, d1+4);
  14.    //
  15.    // Set up four destination vectors.
  16.    //
  17.    vector<int> v3(d2+0, d2+8), v4(d2+0, d2+8), v5(d2+0, d2+8), v6(d2+0, d2+8);
  18.    //
  19.    // Set up one empty vector.
  20.    //
  21.    vector<int> v7;
  22.    //
  23.    // Merge v1 with v2.
  24.    //
  25.    merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
  26.    //
  27.    // Now use comparator.
  28.    //
  29.    merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v4.begin(), less<int>());
  30.    //
  31.    // In place merge v5.
  32.    //
  33.    vector<int>::iterator mid = v5.begin();
  34.    advance(mid,4);
  35.    inplace_merge(v5.begin(),mid,v5.end());
  36.    //
  37.    // Now use a comparator on v6.
  38.    //
  39.    mid = v6.begin();
  40.    advance(mid,4);
  41.    inplace_merge(v6.begin(), mid, v6.end(), less<int>());    
  42.    //
  43.    // Merge v1 and v2 to empty vector using insert iterator.
  44.    //
  45.    merge(v1.begin(), v1.end(), v2.begin(), v2.end(), back_inserter(v7));
  46.    //
  47.    // Copy all cout.
  48.    //
  49.    ostream_iterator<int> out(cout," ");
  50.    copy(v1.begin(),v1.end(),out);
  51.    cout << endl;
  52.    copy(v2.begin(),v2.end(),out);
  53.    cout << endl;
  54.    copy(v3.begin(),v3.end(),out);
  55.    cout << endl;
  56.    copy(v4.begin(),v4.end(),out);
  57.    cout << endl;
  58.    copy(v5.begin(),v5.end(),out);
  59.    cout << endl;
  60.    copy(v6.begin(),v6.end(),out);
  61.    cout << endl;
  62.    copy(v7.begin(),v7.end(),out);
  63.    cout << endl;
  64.    //
  65.    // Merge v1 and v2 to cout.
  66.    //
  67.    merge(v1.begin(),v1.end(),v2.begin(),v2.end(),
  68.          ostream_iterator<int>(cout," "));
  69.    cout << endl;
  70.  
  71.    return 0;
  72.  }
  73.